package org.hbhk.hxl.gateway.controller;

import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang.time.DateUtils;
import org.hbhk.hxl.gateway.model.AccessToken;
import org.hbhk.hxl.gateway.model.AppInfo;
import org.hbhk.hxl.gateway.model.ResultMsg;
import org.hbhk.hxl.gateway.redis.RedisUtil;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AuthController {

	@RequestMapping("/openapi/getToken")
	public ResultMsg apiToken(@RequestBody AppInfo app) {

		ResultMsg data = new ResultMsg();
		AccessToken accessToken = new AccessToken();
		String token = MD5Util.encode(app.getAppId() + "" + System.currentTimeMillis());
		accessToken.setAccessToken(token);

		RedisUtil.put(app.getAppId(), token, 11, TimeUnit.MINUTES);
		accessToken.setExpireTime(DateUtils.addMinutes(new Date(), 10).getTime());

		data.setData(accessToken);
		return data;

	}

	@RequestMapping("/openapi/getToken/test")
	public ResultMsg test() {

		ResultMsg data = new ResultMsg();

		data.setData("成功");
		return data;

	}

	@RequestMapping("/")
	public ResultMsg home() {
		ResultMsg data = new ResultMsg();
		data.setMessage("hello gateway");
		return data;

	}

	@RequestMapping("/favicon.ico")
	public String favicon() {

		return "";

	}

}
